﻿using System;
using System.Web.UI.WebControls;
using SalePartial.BLL;
using XunPan.Web;
using SalePartial.Utility;
using System.Data;
using Telerik.Web.UI;
using System.Web.UI;

public partial class ManageModule : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            int ckint = 0;

            //DateTime ckbtime, cketime;

            // 检查用户权限并存入使用记录。
            LoginUserType type = RoleControlManager.checkUserAuthority(User.Identity.Name);
            if (type != LoginUserType.ADMIN)
            {
                Response.Redirect(GlobalSetting.UrlHomePage, true);
            }

           //DataSet ds = UserBLL.Select_Role( null, null, null);
           //gv_Item1.DataSource = ds;
           //gv_Item1.DataBind();

            ltlItem1.Text = "-1";
            gv_Item2.DataBind();
            
        }
        
    }

    // Check_Power() 检查用户权限
    private void check_power(string f_power)
    {
        // 载入公共函数

        // 若 Session 不存在则直接显示错误消息
        try
        {

            if (SecurityUtility.Check_Power(Session["UserName"].ToString(), Session["mg_power"].ToString(), f_power) > 0)
                Response.Redirect("../Error.aspx?ErrCode=1");
        }
        catch
        {
            Response.Redirect("../Error.aspx?ErrCode=2");
        }
    }

    protected void gv_Item1_PageIndexChanged(object sender, GridViewPageEventArgs e)
    {
        //lb_pageid.Text = gv_Item1.PageIndex.ToString();
    }

    protected void Btn_Set1_Click(object sender, EventArgs e)
    {
        if (tbItem1No.Text.Trim() == string.Empty)
        {
            sqlDSItem1.SelectParameters["fi_no1L"].DefaultValue = "0";
            sqlDSItem1.SelectParameters["fi_no1R"].DefaultValue = "9";
        }
        else
        {
            sqlDSItem1.SelectParameters["fi_no1L"].DefaultValue = tbItem1No.Text;
            sqlDSItem1.SelectParameters["fi_no1R"].DefaultValue = tbItem1No.Text;
        }

        sqlDSItem1.SelectParameters["fi_name1"].DefaultValue =
            tbItem1Nm.Text.Trim() == string.Empty ? "%" : tbItem1Nm.Text.Trim();

        if (ddlItem1Visible.SelectedValue == "-1")
        {
            sqlDSItem1.SelectParameters["is_visibleL"].DefaultValue = "1";
            sqlDSItem1.SelectParameters["is_visibleR"].DefaultValue = "2";
        }
        else
        {
            sqlDSItem1.SelectParameters["is_visibleL"].DefaultValue = ddlItem1Visible.SelectedValue;
            sqlDSItem1.SelectParameters["is_visibleR"].DefaultValue = ddlItem1Visible.SelectedValue;
        }


        gv_Item1.DataBind();
    }

    protected void gv_Item1_UpdateCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {
        GridEditableItem editedItem = (GridEditableItem)e.Item;
        GridEditManager editMan = editedItem.EditManager;

        RadTextBox cbFiNo1 = (RadTextBox)editMan.GetColumnEditor("fi_no1").ContainerControl.FindControl("cbFiNo1");
        RadNumericTextBox cbFiSort1 = (RadNumericTextBox)editMan.GetColumnEditor("fi_sort1").ContainerControl.FindControl("cbFiSort1");
        RadTextBox cbFiName1 = (RadTextBox)editMan.GetColumnEditor("fi_name1").ContainerControl.FindControl("cbFiName1");

        GridDropDownListColumnEditor editor = (GridDropDownListColumnEditor)(editMan.GetColumnEditor("is_visible"));
        //DropDownList ddList = editor.DropDownListControl;

        if (!Utils.IsNumber(cbFiSort1.Text.Trim()))
        {
            gv_Item1.Controls.Add(new LiteralControl("排序只能为数字!"));
            return;
        }


        bool blRt = ModuleItemBLL.UpdateItem1(cbFiNo1.Text.Trim(), Convert.ToInt32(cbFiSort1.Text.Trim()),
                            cbFiName1.Text.Trim(), Convert.ToInt32(editor.SelectedValue.ToString()));

        if (!blRt)
        {
            gv_Item1.Controls.Add(new LiteralControl("‘排序’输入超出范围，请重新修改！"));
            return;
        }
        gv_Item1.DataBind();
    }

    protected void gv_Item1_InsertCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {
        GridEditableItem editedItem = (GridEditableItem)e.Item;
        GridEditManager editMan = editedItem.EditManager;

        RadTextBox cbFiNo1 = (RadTextBox)editMan.GetColumnEditor("fi_no1").ContainerControl.FindControl("cbFiNo1");
        RadNumericTextBox cbFiSort1 = (RadNumericTextBox)editMan.GetColumnEditor("fi_sort1").ContainerControl.FindControl("cbFiSort1");
        RadTextBox cbFiName1 = (RadTextBox)editMan.GetColumnEditor("fi_name1").ContainerControl.FindControl("cbFiName1");

        GridDropDownListColumnEditor editor = (GridDropDownListColumnEditor)(editMan.GetColumnEditor("is_visible"));
        //DropDownList ddList = editor.DropDownListControl;

        if (!Utils.IsNumber(cbFiSort1.Text.Trim()))
        {
            gv_Item1.Controls.Add(new LiteralControl("排序只能为数字!"));
            return;
        }


        bool blRt = ModuleItemBLL.AddItem1(cbFiNo1.Text.Trim(), Convert.ToInt32(cbFiSort1.Text.Trim()),
                            cbFiName1.Text.Trim(), Convert.ToInt32(editor.SelectedValue.ToString()));

        if (!blRt)
        {
            gv_Item1.Controls.Add(new LiteralControl("‘角色名称’存在或者‘排序’超出范围，请重新设置!"));
            return;
        }
        gv_Item1.DataBind();
    }

    protected void gv_Item1_DeleteCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {
        GridItem item = (GridItem)e.Item;
        //        GridDataItem dataItem = (GridDataItem)e.Item;
        //        CheckBox checkbx = (CheckBox)dataItem["IsLockedOut"].Controls[0];
        string fi_no1 = (string)e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["fi_no1"];

        bool blRt = ModuleItemBLL.DeleteItem1(fi_no1);

        if (!blRt)
        {
            gv_Item1.Controls.Add(new LiteralControl("此‘角色名称’已经使用，请重新设置!"));
            return;
        }
        gv_Item1.DataBind();
    }

    protected void gv_Item1_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {
        switch (e.CommandName)
        {
            case "approve":
                //Guid userid = (Guid)e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["UserId"];
                //GridDataItem dataItem = (GridDataItem)e.Item;
                //CheckBox checkbx = (CheckBox)dataItem["IsLockedOut"].FindControl("cbIsLockedOut");
                //bool isLockedOut = checkbx.Checked;
                //checkbx = (CheckBox)dataItem["IsApproved"].FindControl("cbIsApproved");
                //bool isApproved = checkbx.Checked;
                //bool rt = UserBLL.UpdateUser(userid, isLockedOut, isApproved);
                //if (rt)
                //{
                //    //success
                //    e.Item.OwnerTableView.Rebind();

                //}
                //else
                //{
                //}
                break;
            case "refItem2":
                string fi_no1 = (string)e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["fi_no1"];
                ltlItem1.Text = fi_no1;
                gv_Item2.DataBind();
                break;
        }

    }

    protected void gv_Item1_ItemCreated(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridEditableItem && e.Item.IsInEditMode)
        {
            GridEditableItem item = ((GridEditableItem)e.Item);

            if (item.ItemIndex != -1)
            {
                if (item.OwnerTableView.Name == ((RadGrid)sender).MasterTableView.Name)
                {
                    (item.EditManager.GetColumnEditor("fi_no1").ContainerControl.FindControl("cbFiNo1") as RadTextBox).Enabled = false;
                }
            }
        }

    }
    protected void Btn_Set2_Click(object sender, EventArgs e)
    {
        if (tbItem2No.Text.Trim() == string.Empty)
        {
            sqlDSItem2.SelectParameters["fi_no2L"].DefaultValue = "0000";
            sqlDSItem2.SelectParameters["fi_no2R"].DefaultValue = "9999";
        }
        else
        {
            sqlDSItem2.SelectParameters["fi_no2L"].DefaultValue = tbItem2No.Text;
            sqlDSItem2.SelectParameters["fi_no2R"].DefaultValue = tbItem2No.Text;
        }

        sqlDSItem2.SelectParameters["fi_name2"].DefaultValue =
            tbItem2Nm.Text.Trim() == string.Empty ? "%" : tbItem2Nm.Text.Trim();

        if (ddlItem2Visible.SelectedValue == "-1")
        {
            sqlDSItem2.SelectParameters["is_visibleL"].DefaultValue = "2";
            sqlDSItem2.SelectParameters["is_visibleR"].DefaultValue = "1";
        }
        else
        {
            sqlDSItem2.SelectParameters["is_visibleL"].DefaultValue = ddlItem2Visible.SelectedValue;
            sqlDSItem2.SelectParameters["is_visibleR"].DefaultValue = ddlItem2Visible.SelectedValue;
        }


        gv_Item2.DataBind();
    }

    protected void gv_Item2_UpdateCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {
        GridEditableItem editedItem = (GridEditableItem)e.Item;
        GridEditManager editMan = editedItem.EditManager;

        RadTextBox cbFiNo2 = (RadTextBox)editMan.GetColumnEditor("fi_no2").ContainerControl.FindControl("cbFiNo2");
        RadNumericTextBox cbFiSort2 = (RadNumericTextBox)editMan.GetColumnEditor("fi_sort2").ContainerControl.FindControl("cbFiSort2");
        RadTextBox cbFiName2 = (RadTextBox)editMan.GetColumnEditor("fi_name2").ContainerControl.FindControl("cbFiName2");
        RadTextBox cbFiUrl2 = (RadTextBox)editMan.GetColumnEditor("fi_url2").ContainerControl.FindControl("cbFiUrl2");

        GridDropDownListColumnEditor editor = (GridDropDownListColumnEditor)(editMan.GetColumnEditor("is_visible"));
        //DropDownList ddList = editor.DropDownListControl;

        if (!Utils.IsNumber(cbFiSort2.Text.Trim()))
        {
            gv_Item1.Controls.Add(new LiteralControl("排序只能为数字!"));
            return;
        }


        bool blRt = ModuleItemBLL.UpdateItem2(cbFiNo2.Text.Trim(), Convert.ToInt32(cbFiSort2.Text.Trim()),
                            cbFiName2.Text.Trim(), cbFiUrl2.Text.Trim(), Convert.ToInt32(editor.SelectedValue.ToString()));


        if (!blRt)
        {
            gv_Item2.Controls.Add(new LiteralControl("‘排序’输入超出范围，请重新修改!"));
            return;
        }
        gv_Item2.DataBind();
    }


    protected void gv_Item2_InsertCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {
        GridEditableItem editedItem = (GridEditableItem)e.Item;
        GridEditManager editMan = editedItem.EditManager;

        RadTextBox cbFiNo2 = (RadTextBox)editMan.GetColumnEditor("fi_no2").ContainerControl.FindControl("cbFiNo2");
        RadNumericTextBox cbFiSort2 = (RadNumericTextBox)editMan.GetColumnEditor("fi_sort2").ContainerControl.FindControl("cbFiSort2");
        RadTextBox cbFiName2 = (RadTextBox)editMan.GetColumnEditor("fi_name2").ContainerControl.FindControl("cbFiName2");
        RadTextBox cbFiUrl2 = (RadTextBox)editMan.GetColumnEditor("fi_url2").ContainerControl.FindControl("cbFiUrl2");

        GridDropDownListColumnEditor editor = (GridDropDownListColumnEditor)(editMan.GetColumnEditor("is_visible"));
        //DropDownList ddList = editor.DropDownListControl;

        if (!Utils.IsNumber(cbFiSort2.Text.Trim()))
        {
            gv_Item1.Controls.Add(new LiteralControl("排序只能为数字!"));
            return;
        }
        if (cbFiUrl2.Text.Trim() ==string.Empty )
        {
            gv_Item1.Controls.Add(new LiteralControl("程序名字不能够为空!"));
            return;
        }

        bool blRt = ModuleItemBLL.AddItem2(cbFiNo2.Text.Trim(), ltlItem1.Text.Trim(), Convert.ToInt32(cbFiSort2.Text.Trim()),
                            cbFiName2.Text.Trim(),cbFiUrl2.Text.Trim(), Convert.ToInt32(editor.SelectedValue.ToString()));

        if (!blRt)
        {
            gv_Item2.Controls.Add(new LiteralControl("‘排序’输入超出范围，请重新设置!"));
            return;
        }
        gv_Item1.DataBind();
    }

    protected void gv_Item2_DeleteCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {
        GridItem item = (GridItem)e.Item;
        //        GridDataItem dataItem = (GridDataItem)e.Item;
        //        CheckBox checkbx = (CheckBox)dataItem["IsLockedOut"].Controls[0];
        string fi_no2 = (string)e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["fi_no2"];

        bool blRt = ModuleItemBLL.DeleteItem2(fi_no2);

        if (!blRt)
        {
            gv_Item1.Controls.Add(new LiteralControl("‘角色名称’存在或者‘排序’超出范围，请重新设置!"));
            return;
        }
        gv_Item1.DataBind();
    }

    protected void gv_Item2_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {


    }

    
    protected void gv_Item2_ItemCreated(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridEditableItem && e.Item.IsInEditMode)
        {
            GridEditableItem item = ((GridEditableItem)e.Item);

            if (item.ItemIndex != -1)
            {
                if (item.OwnerTableView.Name == ((RadGrid)sender).MasterTableView.Name)
                {

                    (item.EditManager.GetColumnEditor("fi_no2").ContainerControl.FindControl("cbFiNo2") as RadTextBox).Enabled = false;
                }
            }
        }
    }
}
